const express = require('express');
const router = express.Router();
const multer = require('multer');
const { 
  sendVerificationCode, 
  Register, 
  loginWithEmailPassword, 
  loginWithEmailVerification, 
  resetPassword,
  identityVerification,
  updatePassword, 
  updateNickname, 
  updateAvatar, 
  getMe,
  updateProfile,
  getProfile,
  getUserPosts,
} = require('../controllers/userController');

// 配置 multer 用于文件上传
const storage = multer.memoryStorage(); // 使用内存存储
const upload = multer({ storage });

/**
 * @swagger
 * /user/send-verification-code:
 *   post:
 *     summary: 发送邮箱验证码
 *     tags: [User]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *             example:
 *               email: "2654207269@qq.com"
 *     responses:
 *       200:
 *         description: 验证码已发送
 *       400:
 *         description: 邮箱不能为空
 */
router.post('/send-verification-code', sendVerificationCode);

/**
 * @swagger
 * /user/register:
 *   post:
 *     summary: 用户注册
 *     tags: [User]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *               verificationCode:
 *                 type: string
 *               nickname:
 *                 type: string
 *               password:
 *                 type: string
 *               confirmPassword:
 *                 type: string
 *             example:
 *               email: "2654207269@qq.com"
 *               verificationCode: "1956"
 *               nickname: "zzb"
 *               password: "password123"
 *     responses:
 *       201:
 *         description: 注册成功
 *       400:
 *         description: 所有字段均为必填项或验证码错误
 */
router.post('/register', Register);

/**
 * @swagger
 * /user/login:
 *   post:
 *     summary: 用户登录（邮箱+密码）
 *     tags: [User]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *               password:
 *                 type: string
 *             example:
 *               email: "2654207269@qq.com"
 *               password: "newPassword1234"
 *     responses:
 *       200:
 *         description: 登录成功
 *       400:
 *         description: 请输入邮箱和密码
 *       401:
 *         description: 密码错误
 *       404:
 *         description: 用户不存在
 */
router.post('/login', loginWithEmailPassword);

/**
 * @swagger
 * /user/login-with-verification:
 *   post:
 *     summary: 用户登录（邮箱+验证码）
 *     tags: [User]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *               verificationCode:
 *                 type: string
 *             example:
 *               email: "2654207269@qq.com"
 *               verificationCode: "1956"
 *     responses:
 *       200:
 *         description: 登录成功
 *       400:
 *         description: 请输入邮箱和验证码或验证码错误
 */
router.post('/login-with-verification', loginWithEmailVerification);

/**
 * @swagger
 * /user/reset-password:
 *   post:
 *     summary: 重置密码
 *     tags: [User]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *               verificationCode:
 *                 type: string
 *               password:
 *                 type: string
 *             example:
 *               email: "2654207269@qq.com"
 *               verificationCode: "1956"
 *               password: "newPassword123"
 *     responses:
 *       200:
 *         description: 密码已重置
 *       400:
 *         description: 所有字段均为必填项或验证码错误
 */
router.post('/reset-password', resetPassword);

// 身份认证路由，需要用户登录后访问
const { protect } = require('../middlewares/authMiddleware');
router.use(protect);
router.post('/identity-verification', identityVerification);

/**
 * @swagger
 * /user/update-password:
 *   post:
 *     summary: 修改密码
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               oldPassword:
 *                 type: string
 *               newPassword:
 *                 type: string
 *             example:
 *               oldPassword: "newPassword123"
 *               newPassword: "newPassword1234"
 *     responses:
 *       200:
 *         description: 密码修改成功
 *       400:
 *         description: 旧密码不正确或新密码不符合要求
 *       401:
 *         description: 请登录后访问
 */
router.post('/update-password', protect, updatePassword);

/**
 * @swagger
 * /user/update-nickname:
 *   post:
 *     summary: 更新用户昵称
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               nickname:
 *                 type: string
 *             example:
 *               nickname: "zzb"
 *     responses:
 *       200:
 *         description: 昵称更新成功
 *       400:
 *         description: 昵称不能为空
 */
router.post('/update-nickname', protect, updateNickname);

/**
 * @swagger
 * /user/update-avatar:
 *   post:
 *     summary: 更新用户头像
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               avatar:
 *                 type: string
 *                 format: binary
 *     responses:
 *       200:
 *         description: 头像更新成功
 *       400:
 *         description: 请上传头像文件
 */
router.post('/update-avatar', protect, upload.single('avatar'), updateAvatar);

/**
 * @swagger
 * /user/me:
 *   get:
 *     summary: 获取当前用户信息
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 返回当前用户信息
 *       404:
 *         description: 用户不存在
 */
router.get('/me', protect, getMe);

/**
 * @swagger
 * /user/update-profile:
 *   post:
 *     summary: 更新用户头像和昵称
 *     tags: [User]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               nickname:
 *                 type: string
 *               avatar:
 *                 type: string
 *                 format: binary
 *     responses:
 *       200:
 *         description: 用户信息更新成功
 *       400:
 *         description: 请提供至少一个更新字段
 */
router.post('/update-profile', protect, upload.single('avatar'), updateProfile);

// 获取指定用户公开信息
router.get('/:userId', getProfile);
// 获取指定用户的作品（分页）
router.get('/:userId/posts', getUserPosts);

module.exports = router;